package com.itheima.reggie.service.impl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.DTO.DishDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.impl.DishFlavorService;
import com.itheima.reggie.service.impl.DishService;
import com.sun.org.apache.bcel.internal.generic.LADD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.annotation.WebFilter;
import java.beans.Transient;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImp extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存的基本信息到菜品表dish
        this.save(dishDto);
        Long id=dishDto.getId();  //菜品id
        //保存菜品口味数据到dish_flavor saveBatch批量保存集合
        //dishFlavorService.saveBatch(dishDto.getFlavors());
        //菜品口味
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    //根据id查询菜品和口味信息

    @Override
    public DishDto getByIdwithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish=this.getById(id);
        /*拷贝一下对象 因为要的时DishDto对象*/
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息 dish_flavors
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<DishFlavor>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        //为Dto 的flavor值进行赋值
        dishDto.setFlavors(flavors);
        return dishDto;
    }
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据  -----dish_flavor 表delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<DishFlavor>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());//根据传过来的id(Dish_id)删除dish_flavors表里的记录

        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据   ----dish_flavor 表insert操作
        List<DishFlavor> flavors=dishDto.getFlavors();
//        flavors=flavors.stream().map((item)->{
//            item.setDishId(dishDto.getId());
//            return item;
//        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }
}
